package com.homwee.tvap.common.comSettingInterface;

import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.text.TextUtils;
import android.util.Log;

import com.homwee.factory.ExitKeyReceiveActivity;
import com.homwee.factory.FactoryListInfoActivity;
import com.homwee.factory.FactoryModeMenu;
import com.homwee.factory.R;
import com.homwee.factory.TestImagePatternsActivity;
import com.homwee.factory.util.Contants;
import android.provider.Settings;
import android.content.ContentResolver;
//zcj 2018-10-30 import com.changhong.android.tvapi.common.TvManager;
//zcj 2018-10-30 import com.changhong.android.tvapi.common.exception.TvCommonException;
//import com.changhong.itv.TvInfoCollect.InfoCollectFile;
//zcj 2018-10-30 import com.changhong.android.tvapi.common.vo.ChOsType;
//zcj 2018-10-30 import com.changhong.android.tvapi.common.vo.ChOsType.CHENUMColorTempMode;
//zcj 2018-10-30 import com.changhong.android.tvapi.common.vo.ChOsType.CHENUMPictureMode;
//zcj 2018-10-30 import com.changhong.android.tvapi.common.vo.ChOsType.CHEnumHDMINonStandard;
//zcj 2018-10-30 import com.changhong.android.tvapi.common.vo.EnumInputSource;

import com.homwee.tvap.common.srvfunc.FactorySetting;
import com.mediatek.twoworlds.factory.MtkTvFApiDisplay;
//zcj 2018-10-30 import com.changhong.tvos.service.TvChannelManager;
//zcj 2018-10-30 import com.changhong.tvos.service.TvCommonManager;
//zcj 2018-10-30 import com.changhong.tvos.service.TvFactoryManager;
//zcj 2018-10-30 import com.changhong.tvos.service.TvPictureManager;
//import com.changhong.tvos.common.MiscManager;
//import com.changhong.tvos.common.SystemManager;
//import com.changhong.tvos.common.TVManager;
//import com.changhong.tvos.common.PictureManager;
//import com.changhong.tvos.model.PanelInfo;
//import com.changhong.android.tvapi.common.vo.ChOsType.CHEnumHDMINonStandard;
//zcj 2018-10-30 import com.changhong.tvos.service.TvMiscManager;
import com.mediatek.twoworlds.factory.MtkTvFApiDisplayTest;
import com.mediatek.twoworlds.factory.common.MtkTvFApiDisplayTypes;
import com.mediatek.twoworlds.factory.MtkTvFApiInformation;
import com.mediatek.twoworlds.factory.MtkTvFApiInputSource;
import com.mediatek.twoworlds.factory.MtkTvFApiPeripheral;
import com.mediatek.twoworlds.factory.MtkTvFApiSystem;
import com.mediatek.twoworlds.factory.common.MtkTvFApiDisplayTypes.ColorTempData;
import com.mediatek.twoworlds.factory.common.MtkTvFApiDisplayTypes.EnumColorTemperature;
import com.mediatek.twoworlds.factory.common.MtkTvFApiSystemTypes;
import com.mediatek.twoworlds.tv.MtkTvConfig;
import com.mediatek.twoworlds.tv.MtkTvInputSource;
import com.mediatek.twoworlds.tv.MtkTvNetwork;
import com.mediatek.twoworlds.tv.MtkTvUtil;
import com.mediatek.twoworlds.tv.common.MtkTvConfigTypeBase;
import com.mediatek.twoworlds.factory.model.MtkTvFApiSysFactorySvcCmd_e;
import com.mediatek.twoworlds.tv.MtkTvBroadcast;

//for local dimming vop pattern
import com.mediatek.twoworlds.factory.MtkTvFApiDisplayTest;
import com.mediatek.twoworlds.factory.model.MtkTvFApiDispTstPattern;

//page 5
public class factoryPage5 {
	private Context mContext;
	static factoryPage5 Interface = null;

	MtkTvFApiSystem mMtkTvFApiSystem = null;
	MtkTvFApiDisplayTest mMtkTvFApiDisplayTest = null;
	MtkTvInputSource mMtkTvInputSource = null;
	private MtkTvFApiInputSource mTvFApiInputSource = null;
	MtkTvConfig mMtkTvConfig = null;
	MtkTvNetwork mMtkTvNetwork = null;
	MtkTvFApiPeripheral mMtkTvFApiPeripheral = null;
	MtkTvUtil mMtkTvUtil = null;
	private MtkTvFApiDisplay mMtkTvFApiDisplay = null;

	private static final String TAG = "Factory 5";
        private ContentResolver mContentResolver;
        private String mBrand;
	

	private factoryPage5(Context context) {
		// TVManager tvManager = TVManager.getInstance(context);
		try {
			initSourceName();
			mContext = context;
			mMtkTvFApiSystem = MtkTvFApiSystem.getInstance();
			mMtkTvFApiDisplayTest = MtkTvFApiDisplayTest.getInstance();
			mMtkTvInputSource = MtkTvInputSource.getInstance();
			mMtkTvConfig = MtkTvConfig.getInstance();
			mMtkTvFApiPeripheral = MtkTvFApiPeripheral.getInstance();
			mMtkTvNetwork = MtkTvNetwork.getInstance();
			mMtkTvUtil = MtkTvUtil.getInstance();
			mTvFApiInputSource = MtkTvFApiInputSource.getInstance();
			mMtkTvFApiDisplay=MtkTvFApiDisplay.getInstance();
                        mContentResolver = context.getContentResolver();
                        mBrand = android.os.Build.BRAND;
		} catch (Exception e) {
			// TODO: handle exception
			Log.v("tv",
					"miscManager ==null or pictureManager==null or systemManager==null");
		}
	}

	public static factoryPage5 getInstance(Context context) {
		if (Interface == null) {
			Interface = new factoryPage5(context);
		}
		return Interface;
	}

	ArrayList<MtkTvInputSource.InputSourceRecord> inputList = new ArrayList<MtkTvInputSource.InputSourceRecord>();
	ArrayList<String> mUiSourceList = new ArrayList<String>();
	//Map<String, String> sourceMap = new HashMap<String, String>();

	private void initSourceName() {

		int compositeNum = 0;
		int size = MtkTvInputSource.getInstance().getInputSourceTotalNumber();

		for (int i = 0; i < size; i++) {
			MtkTvInputSource.InputSourceRecord record = new MtkTvInputSource.InputSourceRecord();
			MtkTvInputSource.getInstance().getInputSourceRecbyidx(i, record);

			inputList.add(record);
			String sourceTypeName;
			sourceTypeName = record.getInputType().name();

			if (TextUtils.isEmpty(sourceTypeName)) {
				continue;
			} else {

				if (sourceTypeName.equalsIgnoreCase("TV")) {
					if (mUiSourceList.contains("DTV")) {
						mUiSourceList.add("ATV");
						//sourceMap.put("ATV", Contants.ATVUri);
					} else {
						mUiSourceList.add("DTV");
						//sourceMap.put("DTV", Contants.DTVUri);
					}
				} else if (sourceTypeName.equalsIgnoreCase("COMPOSITE")) {
					compositeNum = record.getInternalIdx();
					if (compositeNum > 0) {// modify string for idx=0
						if (compositeNum == 1) {
							mUiSourceList.set(mUiSourceList.size() - 1,
									"Composite " + compositeNum);
						}
						mUiSourceList.add("Composite " + (compositeNum + 1));
					} else {
						mUiSourceList.add("Composite");
						//sourceMap.put("Composite", Contants.CompositeUri);
					}
				} else if (sourceTypeName.equalsIgnoreCase("COMPONENT")) {
					mUiSourceList.add("Component");
					//sourceMap.put("Component", Contants.ComponentUri);
				} else if (sourceTypeName.equalsIgnoreCase("VGA")) {
					mUiSourceList.add("VGA");
					//sourceMap.put("VGA", Contants.VgaUri);
				} else if (sourceTypeName.equalsIgnoreCase("HDMI")) {
					// if ((record.getInternalIdx() + 1) ==
					// mTvInputSource.getMhlPortNum()) {//
					// mTvInputSource.getMhlPortNum()
					// mUiSourceList.add("HDMI "
					// + (record.getInternalIdx() + 1) + " / MHL");
					// } else {
					mUiSourceList.add("HDMI " + (record.getInternalIdx() + 1));
					// }
					if (record.getInternalIdx() + 1 == 1) {
						//sourceMap.put("HDMI 1", Contants.HDMI1Uri);
					} else if (record.getInternalIdx() + 1 == 2) {
						//sourceMap.put("HDMI 2", Contants.HDMI2Uri);
					} else if (record.getInternalIdx() + 1 == 3) {
						//sourceMap.put("HDMI 3", Contants.HDMI3Uri);
					} else if (record.getInternalIdx() + 1 == 4) {
						//sourceMap.put("HDMI 4", Contants.HDMI4Uri);
					}
				} else if (sourceTypeName.equalsIgnoreCase("RESERVED")) {
					mUiSourceList.add("RESERVED");//cancel this source
				} else if (sourceTypeName.equalsIgnoreCase("SCART")) {
					mUiSourceList.add("SCART");//cancel this source
				} else if (sourceTypeName.equalsIgnoreCase("SVIDEO")) {
					mUiSourceList.add("SVIDEO");//cancel this source
				}else{
					mUiSourceList.add("Other");
				}
			}
		}
	}

	public int getSourceIndex() {
		String srcId="";
		//String[] srcArray =;

		srcId = MtkTvInputSource.getInstance().getCurrentInputSourceName();
		Log.v("TV", "Get Current Source" + srcId);
		if(srcId.equalsIgnoreCase("HDMI 1")
				||srcId.equalsIgnoreCase("HDMI 2")
				||srcId.equalsIgnoreCase("HDMI 3")
				||srcId.equalsIgnoreCase("HDMI 4")){
			return 0;
		}else if(srcId.equalsIgnoreCase("Composite")) {
			return 3;
		}else if(srcId.equalsIgnoreCase("Component")) {
			return 1;
		}else if(srcId.equalsIgnoreCase("ATV")) {
			return 5;
		}else if(srcId.equalsIgnoreCase("DTV")) {
			return 6;
		}else if(srcId.equalsIgnoreCase("VGA")) {
			return 2;
		}else if(srcId.equalsIgnoreCase("SCART")) {
			return 4;
		}else{
			return 0;
		}

		//return 0;

	}
	/*E_HDMI("HDMI"),
                    E_YPBPR("YPBPR"),
                    E_VGA("VGA"),
                    E_CVBS("CVBS"),
                    E_SVIDEO("SVIDEO"),
                    E_SCART("SCART"),
                    E_ATV("ATV"),
                    E_DTV("DTV"),
                    E_OTHERS("OTHERS")*/
	public int getSourceIndexForColor() {
		String srcId="";
		//String[] srcArray =;

		srcId = MtkTvInputSource.getInstance().getCurrentInputSourceName();
		Log.v("TV", "Get Current Source" + srcId);
		if(srcId.equalsIgnoreCase("HDMI 1")
				||srcId.equalsIgnoreCase("HDMI 2")
				||srcId.equalsIgnoreCase("HDMI 3")
				||srcId.equalsIgnoreCase("HDMI 4")){
			return 0;
		}else if(srcId.equalsIgnoreCase("Composite")) {
			return 3;
		}else if(srcId.equalsIgnoreCase("Component")) {
			return 1;
		}else if(srcId.equalsIgnoreCase("ATV")) {
			return 6;
		}else if(srcId.equalsIgnoreCase("DTV")) {
			return 7;
		}else if(srcId.equalsIgnoreCase("VGA")) {
			return 2;
		}else if(srcId.equalsIgnoreCase("SCART")) {
			return 5;
		}else{
			return 0;
		}

		//return 0;

	}

	public String[] getSourceList(){
		String[] strs={"HDMI", "YPBPR", "VGA", "CVBS", "SCART", "ATV", "DTV"};
		return strs;
	}

	public void setSourceIndex(int value) {
		// {"HDMI", "YPBPR", "VGA", "CVBS", "SCART", "ATV", "DTV"}
		switch (value) {
		case 0:
			changeSource(mContext, "HDMI 1");
			break;
		case 1:
			changeSource(mContext, "Component");
			break;
		case 2:
			changeSource(mContext, "VGA");
			break;
		case 3:
			changeSource(mContext, "Composite");
			break;
		case 4:
			changeSource(mContext, "SCART");
			break;
		case 5:
			changeSource(mContext, "ATV");
			break;
		case 6:
			changeSource(mContext, "DTV");
			break;

		default:
			break;
		}

	}

	public int getColorTempMode() {
		int flag = 1;
		flag = mMtkTvConfig
				.getConfigValue(MtkTvConfigTypeBase.CFG_VIDEO_CLR_TEMP) - 1;
		return flag >= 3 ? 1 : flag;
	}

	public void setColorTempMode(int value) {
		int colorTemp = (value) % 3;
		if (mMtkTvConfig != null) {
			mMtkTvConfig.setConfigValue(MtkTvConfigTypeBase.CFG_VIDEO_CLR_TEMP,
					colorTemp + 1);
		}
		if(mMtkTvFApiDisplay!=null){
			mMtkTvFApiDisplay.saveDisplayIni();
		}
		
	}

	public int getRGain() {
		// return mTvMiscManager.getRGAIN();
		ColorTempData rGain = null;
		rGain = MtkTvFApiDisplay
				.getInstance()
				.getWbGainOffsetEx(
						MtkTvFApiDisplayTypes.EnumColorTemperature.values()[mMtkTvConfig
								.getConfigValue(MtkTvConfigTypeBase.CFG_VIDEO_CLR_TEMP)],
						getSourceIndex());
		/*
		 * return mMtkTvConfig
		 * .getConfigValue(MtkTvConfigTypeBase.CFG_VIDEO_CLR_GAIN_R);
		 */

		return rGain.redGain;
	}

	public ColorTempData getColorTempData(MtkTvFApiDisplayTypes.EnumColorTemperature enumColorTemperature){
		ColorTempData mColorTempData = null;
		mColorTempData = MtkTvFApiDisplay
				.getInstance()
				.getWbGainOffsetEx(enumColorTemperature,
						getSourceIndexForColor());
		/*
		 * return mMtkTvConfig
		 * .getConfigValue(MtkTvConfigTypeBase.CFG_VIDEO_CLR_GAIN_R);
		 */

		return mColorTempData;
	}
	public int getGGain() {
		// return mTvMiscManager.getGGAIN();
		return mMtkTvConfig
				.getConfigValue(MtkTvConfigTypeBase.CFG_VIDEO_CLR_GAIN_G);
	}

	public int getBGain() {
		// return mTvMiscManager.getBGAIN();
		return mMtkTvConfig
				.getConfigValue(MtkTvConfigTypeBase.CFG_VIDEO_CLR_GAIN_B);
	}

	public void setRGain(int value) {
		mMtkTvConfig.setConfigValue(MtkTvConfigTypeBase.CFG_VIDEO_CLR_GAIN_R,
				value);
	}

	public void setWbGain(EnumColorTemperature eColorTemp,ColorTempData mColorTempData) {
		mMtkTvFApiDisplay.setWbGainOffsetEx(eColorTemp, mColorTempData, getSourceIndexForColor());
		
		mMtkTvFApiDisplay.saveDisplayIni();
	}
	public void setGGain(int value) {
		mMtkTvConfig.setConfigValue(MtkTvConfigTypeBase.CFG_VIDEO_CLR_GAIN_G,
				value);
	}

	public void setBGain(int value) {
		mMtkTvConfig.setConfigValue(MtkTvConfigTypeBase.CFG_VIDEO_CLR_GAIN_B,
				value);
	}
	public void setBGainOffset(int value) {
		mMtkTvConfig.setConfigValue(MtkTvConfigTypeBase.CFG_VIDEO_CLR_OFFSET_B,
				value);
	}
	public void setRGainOffset(int value) {
		mMtkTvConfig.setConfigValue(MtkTvConfigTypeBase.CFG_VIDEO_CLR_OFFSET_R,
				value);
	}
	public void setGGainOffset(int value) {
		mMtkTvConfig.setConfigValue(MtkTvConfigTypeBase.CFG_VIDEO_CLR_OFFSET_G,
				value);
	}

	public int getPowerMode() {
		int powerMode = 0;
		if (mMtkTvFApiSystem != null) {
			powerMode = mMtkTvFApiSystem.getFactoryPowerMode().ordinal();
			Log.v("tv", "getPowerMode =" + powerMode);
			powerMode = (powerMode) % 3;
		}
		return powerMode;
	}

	public int getNonStandardFlag() {
		int flag = 0;
		/*
		 * //zcj 2018-10-30 if(mTvMiscManager !=null){ //flag
		 * =miscManager.getDriverOperate
		 * (ENUMDriverOperate.CHTV_MW_CUST_SPEC_NONSTARDARD_SWITCH_ONOFF
		 * .ordinal(), 1); }
		 */

		return flag;
	}

	public int getDebugFlag() {
		int flag = 0;
		/*
		 * //zcj 2018-10-30 if(mTvMiscManager !=null){ flag
		 * =mTvMiscManager.getDebugLevel(); }
		 */
		return flag;
	}

	public int getLRMirror() {
		boolean flag = false;
		/*
		 * if(mTvMiscManager !=null){ flag =mTvMiscManager.getPanelLRMirror(); }
		 */
		if (flag) {
			return 0;
		} else {
			return 1;
		}

	}

	public int getUDMirror() {
		boolean flag = false;
		/*
		 * //zcj 2018-10-30 if(mTvMiscManager !=null){ flag
		 * =mTvMiscManager.getPanelUDMirror(); }
		 */
		if (flag) {
			return 0;
		} else {
			return 1;
		}
	}

	public void setPowerMode(int powerMode) {
		if (mMtkTvFApiSystem != null) {
			switch (powerMode) {
			case 2:
				mMtkTvFApiSystem
						.setFactoryPowerMode(MtkTvFApiSystemTypes.EnumSystemFactoryPowerMode.E_MTK_FAPI_SYSTEM_FACTORY_POWER_MODE_DIRECT);
				break;
			case 0:
				mMtkTvFApiSystem
						.setFactoryPowerMode(MtkTvFApiSystemTypes.EnumSystemFactoryPowerMode.E_MTK_FAPI_SYSTEM_FACTORY_POWER_MODE_SECONDARY);
				break;
			case 1:
				mMtkTvFApiSystem
						.setFactoryPowerMode(MtkTvFApiSystemTypes.EnumSystemFactoryPowerMode.E_MTK_FAPI_SYSTEM_FACTORY_POWER_MODE_MEMORY);
				break;
			}

		}
	}

	public void setNonStandardFlag(int value) {
		int flag[] = { (value + 1) % 2 };
		/*
		 * //zcj 2018-10-30 if(mTvMiscManager !=null){
		 * //miscManager.setDriverOperate
		 * (ENUMDriverOperate.CHTV_MW_CUST_SPEC_NONSTARDARD_SWITCH_ONOFF
		 * .ordinal(), flag.length,flag); }
		 */

	}

	public void setDebugFlag(int value) {
		int flag[] = { value };
		/*
		 * //zcj 2018-10-30 if(mTvMiscManager !=null){
		 * //miscManager.setDriverOperate
		 * (ENUMDriverOperate.CHTV_MW_CUST_SPEC_DEBUG_ONOFF
		 * .ordinal(),flag.length, flag); mTvMiscManager.setDebugLevel(value); }
		 */

	}

	public void setLRMirror(int value) {
		boolean bMirror = false;
		if (value == 0) {
			bMirror = true;
		}
		/*
		 * //zcj 2018-10-30 if(mTvMiscManager !=null){
		 * mTvMiscManager.setPanelLRMirror(bMirror); }
		 */

	}

	public void setUDMirror(int value) {
		boolean bMirror = false;
		if (value == 0) {
			bMirror = true;
		}

		/*
		 * //zcj 2018-10-30 if(mTvMiscManager !=null){
		 * mTvMiscManager.setPanelUDMirror(bMirror); }
		 */

	}

	public void resetEEP() {
		/*
		 * //zcj 2018-10-30 if(mTvMiscManager !=null){
		 * //mTvMiscManager.resetEEP(); Log.v("factory","resetEEP=initEEP");
		 * mTvMiscManager.initEEP(); }
		 */

	}

	public int setClearNetActive(String mInputStr) {
		// InfoCollectFile mInfoCollectFile;
		// mInfoCollectFile = InfoCollectFile.getInstance(mContext);
		// int ret =0;
		// if(mInputStr ==null){
		// ret = mInfoCollectFile.send("POSTACTIVE", "0");
		// }else{
		// ret = mInfoCollectFile.send("POSTACTIVE", "0" ,mInputStr);
		// }
		//
		return 0;// ret;
	}

	public void setClearApp() {
		/*
		 * if(mTvMiscManager !=null){ // miscManager.clearApp(); }
		 */

	}

	public void setAgainLand() {
		/*
		 * if(mTvMiscManager !=null){ // miscManager.reEnter(); }
		 */

	}

	public int getHDMINonstdType() {
		// TODO Auto-generated method stub
		// ChOsType.CHEnumHDMINonStandard
		// zcj 2018-10-30 int type
		// =mTvMiscManager.getHDMINonstdType().ordinal();
		int type = 0;
		Log.v("tv", "getHDMINonstdType =" + type);
		return type;

	}

	public void setHDMINonstdType(int value) {
		// TODO Auto-generated method stub
		Log.v("tv", "setHDMINonstdType =" + value);

		// zcj 2018-10-30
		// mTvMiscManager.setHDMINonstdType(ChOsType.CHEnumHDMINonStandard.values()[value]);
	}

	public int getAgingModel() {
		// TODO Auto-generated method stub
		int mode = 0;
		mode = MtkTvFApiInformation
				.getInstance()
				.getIniParameter(Contants.KEYSTATUS_INI_FILEPATH,
						Contants.FACTORY_AGE_MODE).equalsIgnoreCase("1") ? 1
				: 0;
		/*
		 * try { mode =
		 * Settings.System.getInt(mContext.getContentResolver(),Contants
		 * .FACTORY_AGE_MODE_GO); } catch (SettingNotFoundException e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); }
		 */
		Log.v("tv", "in getAgingModel()=="+mode);
		if(mode==1){
			int backlightInt=1;
			int pictureModeInt=1;
			int dreamTimeInt=1;
			int twoHourInt=7200000;
			try{
				backlightInt = Settings.Global.getInt(mContext.getContentResolver(), "picture_backlight");
				//pictureModeInt=Settings.Global.getInt(mContext.getContentResolver(), "picture_mode");
				dreamTimeInt=Settings.System.getInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT);
			}catch (SettingNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(backlightInt!=100||dreamTimeInt!=twoHourInt){
				//Settings.Global.putInt(mContext.getContentResolver(), "picture_mode", 0);
				Settings.Global.putInt(mContext.getContentResolver(), "picture_backlight", 100);
				Settings.System.putInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT, twoHourInt);
			}
		}

		return mode;
	}

	public int getPVRRecordAll() {
		// TODO Auto-generated method stub
		Log.v("tv", "in getRecordAll()");
		// return mTvMiscManager.GetFACTORY_AM();
		// return mTvMiscManager.getAgingMode();
		// zcj 2018-10-30 return mTvFactoryManager.getPvrRecordAll()?1:0;
		return 0;
	}

	public int getKeyUpgradeForce() {
		// TODO Auto-generated method stub
		int force = 0;
		force = MtkTvFApiInformation
				.getInstance()
				.getIniParameter(Contants.PATH_FACTORYINI,
						Contants.KEY_UPGRADE_FORCE).equalsIgnoreCase("1") ? 1
				: 0;
		/*
		 * try {
		 * force=Settings.System.getInt(mContext.getContentResolver(),Contants
		 * .KEY_UPGRADE_FORCE_GO); } catch (SettingNotFoundException e) { //
		 * TODO Auto-generated catch block force=0; }
		 */

		Log.v("tv", "force " + force);
		return force;
	}

	public boolean setKeyUpgradeForce(int value) {
		// TODO Auto-generated method stub
		Log.v("tv", "setKeyUpgradeForce " + value);
		boolean force = false;
		force = MtkTvFApiInformation.getInstance().setIniParameter(
				Contants.PATH_FACTORYINI, Contants.KEY_UPGRADE_FORCE,
				"" + value) != -1 ? true : false;
		// force=Settings.System.putInt(mContext.getContentResolver(),
		// Contants.KEY_UPGRADE_FORCE_GO, value);

		return force;
	}
	
	public int getIsFirstPowerON() {
		// TODO Auto-generated method stub
		int force = 0;
		force = MtkTvFApiInformation
				.getInstance()
				.getIniParameter(Contants.PATH_FACTORYINI,
						Contants.FACTORY_FIRST_POWERON).equalsIgnoreCase("1") ? 1
				: 0;
		/*
		 * try {
		 * force=Settings.System.getInt(mContext.getContentResolver(),Contants
		 * .KEY_UPGRADE_FORCE_GO); } catch (SettingNotFoundException e) { //
		 * TODO Auto-generated catch block force=0; }
		 */

		Log.v("tv", "force " + force);
		return force;
	}

	public boolean setFirstPowerON(int value) {
		// TODO Auto-generated method stub
		Log.v("tv", "setFirstPowerON " + value);
		boolean force = false;
                if(mBrand.equalsIgnoreCase("Philco")){
		force = MtkTvFApiInformation.getInstance().setIniParameter(
				Contants.PATH_FACTORYINI, Contants.FACTORY_FIRST_POWERON,
				"" + value) != -1 ? true : false;
                }
		// force=Settings.System.putInt(mContext.getContentResolver(),
		// Contants.KEY_UPGRADE_FORCE_GO, value);

		return force;
	}


	public int getSameService() {
		// TODO Auto-generated method stub
		Log.v("tv", "getSameServiceSwitchStatus");
		// zcj 2018-10-30 return mTvChannelManager.getSameServiceSwitchStatus();
		return 0;
	}

	public void setPVRRecordAll(int value) {
		// TODO Auto-generated method stub
		Log.v("tv", "in getRecordAll()");
		// zcj 2018-10-30
		// mTvFactoryManager.setPvrRecordAll(value==1?true:false);
	}

	public void setSameService(int value) {
		// TODO Auto-generated method stub
		Log.v("tv", "in setSameServiceSwitchStatus()");
		// zcj 2018-10-30 mTvChannelManager.setSameServiceSwitchStatus(value);

	}

	public void setAgingModel(int value) {
		// TODO Auto-generated method stub
		Log.v("tv", "in setAgingModel()");

		Settings.Global.putInt(mContext.getContentResolver(), "picture_mode", 0);
		Settings.System.putInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT, 7200000);
		Settings.Global.putInt(mContext.getContentResolver(), "picture_backlight", 100);
		Log.d("CHFactory.SetAging","SET PicMode");

		//VOP pattern
		MtkTvFApiDispTstPattern patternMode = new MtkTvFApiDispTstPattern();
		patternMode.mIsVopEnable = value==1?true:false;
		patternMode.mWindow = 0;
		int ret=MtkTvFApiDisplayTest.getInstance().generateTestPattern(6, patternMode);//
		Log.d("Testpattern",ret+"");


		mMtkTvFApiDisplayTest.setBurningMode(value == 1 ? true : false);
		if (value == 1) {
			//for fix local dimming project
			if(factoryPage1.getInstance(mContext).getLDMEnable()==1) {
				Intent imageActivity = new Intent(mContext, TestImagePatternsActivity.class);
				imageActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				imageActivity.putExtra("agingPattern",1);
				mContext.startActivity(imageActivity);
			}

			Settings.Global.putInt(mContentResolver, Settings.Global.STAY_ON_WHILE_PLUGGED_IN,1);
			MtkTvUtil.IRRemoteControl(4);
		} else {
			if(factoryPage1.getInstance(mContext).getLDMEnable()==1) {
			Log.d("factoryPage5.Testpattern","finish");
			((Activity)TestImagePatternsActivity.mContext).finish();
			}
			Settings.Global.putInt(mContentResolver, Settings.Global.STAY_ON_WHILE_PLUGGED_IN,0);
			MtkTvUtil.IRRemoteControl(3);
			MtkTvFApiInformation.getInstance().setIniParameter(
					Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_SKIP_WIZARDSETUP, "" + 0);
		}
		MtkTvFApiInformation.getInstance()
				.setIniParameter(Contants.KEYSTATUS_INI_FILEPATH,
						Contants.FACTORY_AGE_MODE, "" + value);
		// boolean ret=
		// Settings.System.putInt(mContext.getContentResolver(),Contants.FACTORY_AGE_MODE_GO,value);

		FactoryModeMenu.refresh(mContext);
	}

	public int getWhiteBalance() {
		// TODO Auto-generated method stub
		// zcj 2018-10-30 Boolean tmp =
		// mTvMiscManager.Get_Uart_Communication_Flag();
		// int tmp = mTvMiscManager.getUartEnable();
		// int ret = -1;
		// if(tmp == true){
		// ret = 1;
		// }else if(tmp == false){
		// ret = 0;
		// }
		// zcj 2018-10-30 Log.v("tv", "in getWhiteBalance() = " + tmp);

		// zcj 2018-10-30 return tmp?1:0;
		return 1;
	}

	public void setWhiteBalance(int value) {
		// TODO Auto-generated method stub
		Log.v("tv", "in setWhiteBalance()1-" + value);
		// zcj 2018-10-30 mTvMiscManager.Set_Uart_Communication_Flag(value);
		// mTvMiscManager.setPictureMode(CHENUMPictureMode.CH_PICTURE_MODE_VIVID);
		// mTvMiscManager.setColorTempture(CHENUMColorTempMode.CH_COLOR_TEMP_COOL);
		FactoryModeMenu.refresh(mContext);

	}

	public int getUartOnOff() {

		// return mTvFactoryManager.getUartOnOff()?1:0;
		// mMtkTvFApiPeripheral.
		return mMtkTvFApiSystem.getUartEnvironment() ? 1 : 0;
	}

	public void setUartOnOff(int value) {

		// mTvFactoryManager.setUartOnOff((value!=0)?true:false);
		mMtkTvFApiSystem.setUartEnvironment(value == 1 ? true : false);
	}

	public void setNetworkAdb(int value) {

		int command = MtkTvFApiSysFactorySvcCmd_e.E_MTK_TV_FAPI_SYS_FACTORY_CMD_SYS_CALL;
		String parameter = "echo "+ value +" >/sys/devices/Mstar-otg/software_id";
		if(mMtkTvFApiSystem!=null) {
			mMtkTvFApiSystem.sendCmdToFactorySvc(command, parameter);
		}
	}

	public int getNetworkAdb() {
		String tempData=mMtkTvFApiSystem.readFile(Contants.FACTORY_PAGE5_NETWORK_ADB_FILE,100,0);
		Log.d("zcj","getNetworkAdb: "+tempData);
		if(TextUtils.isEmpty(tempData)){
			return 0;
		}
		else{
			Log.d("zcj", "getNetworkAdb: "+(tempData.substring(0,1).equalsIgnoreCase("0")?0:1));
			return tempData.substring(0,1).equalsIgnoreCase("0")?0:1;
		}
	}
	public int getWatchDogMode() {
		return mMtkTvFApiSystem.getWatchDogStatus() ? 1 : 0;
	}

	public void setWatchDogMode(int value) {
		mMtkTvFApiSystem.setWatchDog((value != 0) ? true : false);
	}

	public int getFunctionPresetByIndex(byte index) {
		final int address = 0x1f70;
		byte[] bdata = new byte[1];
		// zcj 2018-10-30 mTvMiscManager.readEEpByAddr(address+index, 1,
		// bdata);//(address+index , bdata, 1);
		Log.v("factory", "value=" + bdata[0]);
		return bdata[0];
	}

	public void setFunctionPresetByIndex(byte index, int value) {

		final int address = 0x1f70;
		byte[] bdata = new byte[1];
		bdata[0] = (byte) value;
		// zcj 2018-10-30 mTvMiscManager.writeEEpByAddr(address+index, 1,
		// bdata);//(address+index , bdata, 1);
		Log.v("factory", "value=" + bdata[0]);
	}

	public void setFirstPowerPanelFlag(int value) {

		final int address = 0x20f;
		byte[] bdata = new byte[1];
		bdata[0] = (byte) value;
		// zcj 2018-10-30 mTvMiscManager.writeEEpByAddr(address, 1,
		// bdata);//(address+index , bdata, 1);
		Log.v("factory", "value=" + bdata[0]);
	}

	public int getWIFIOnOff() {
		return mMtkTvNetwork.getWifiWolCtl() ? 1 : 0;
		//return 0;
	}

	public void setWIFIOnOff(int value) {
		mMtkTvNetwork.setWifiWolCtl(value == 1 ? true : false);
	}

	public int getWOWOnOff() {
		return mMtkTvNetwork.getEthernetWolCtl() ? 1 : 0;
		//return 0;
	}

	public void setWOWOnOff(int value) {
		mMtkTvNetwork.setEthernetWolCtl(value == 1 ? true : false);
	}

	public boolean EnableUartDebug() {
		// zcj 2018-10-30 return mTvFactoryManager.enableUartDebug();
		return false;
	}

	public void changeSource(Context context, String str) {
        Log.d("zcjtest",str);
		if (mUiSourceList.contains(str)) {
            Log.d("zcjtest2",str);
			Intent tmp = new Intent(Intent.ACTION_VIEW);
			tmp.setAction("com.mediatek.select.source");
			tmp.putExtra("com.mediatek.select.sourcename", str);
			context.sendBroadcast(tmp);
		}
	}

}